﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sitecore.Caching;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Data.Templates;
using Sitecore.Data;

namespace Sitecore.ChangesTracker.Caching
{
    class TemplateHashCache : CustomCache
    {
        public TemplateHashCache(string name, long maxSize)
            : base(name, maxSize)
        {
        }
        public void AddRecord(TemplateItem template, int hash)
        {
            this.AddRecord(template.ID, hash);
        }
        public void AddRecord(ID templateId, int hash)
        {
            base.InnerCache.Add(templateId, hash.ToString());
        }

        public void ClearForTemplate(Template template)
        {
            RemoveRecord(template.ID);
            foreach (Template t in template.GetDescendants())
            {
                RemoveRecord(t.ID);
            }            
        }

        

        public void RemoveRecord(ID TemplateId)
        {
            base.InnerCache.Remove(TemplateId);
        }

        public int? GetRecord(ID TemplateId)
        {
            string value=base.InnerCache.GetValue(TemplateId) as string;
            if(!string.IsNullOrEmpty(value))
            {
                return int.Parse(value);
            }
            return null;            
        }
    }
}
